package com.system.cacheCenterBackend.controller.redisDataController;

import com.system.cacheCenterBackend.controller.hotKeyController.HotKeyController;
import com.system.cacheCenterBackend.service.ListCacheService;
import com.system.cacheClient.common.CacheR;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * List缓存控制器
 * 提供List类型的RESTful API接口
 */
@RestController
@RequestMapping("/cache/list")
@RequiredArgsConstructor
public class ListCacheController {

    private final ListCacheService listCacheService;

    /**
     * 设置List缓存值
     *
     * @param key   List键
     * @param value List值
     * @return 统一返回结果
     */
    @PostMapping("/{key}")
    public CacheR<Boolean> set(@PathVariable String key, @RequestBody List<String> value) {
        try {
            if (listCacheService.leftPushAll(key, value) != 0) {
                // 添加键到热点数据控制器中
                HotKeyController.addKey("list", key);
                return CacheR.success(true, "设置List缓存成功");
            } else {
                return CacheR.fail("设置List缓存失败");
            }
        } catch (Exception e) {
            return CacheR.fail("设置List缓存异常: " + e.getMessage());
        }
    }

    /**
     * 获取List缓存值
     *
     * @param key List键
     * @return 统一返回结果
     */
    @GetMapping("/{key}")
    public CacheR<List<String>> get(@PathVariable String key) {
        try {
            List<String> value = listCacheService.get(key);
            if (value != null) {
                return CacheR.success(value, "获取List缓存成功");
            } else {
                return CacheR.fail(404, "List缓存不存在");
            }
        } catch (Exception e) {
            return CacheR.fail("获取List缓存异常: " + e.getMessage());
        }
    }

    /**
     * 删除List缓存值
     *
     * @param key List键
     * @return 统一返回结果
     */
    @DeleteMapping("/{key}")
    public CacheR<Boolean> delete(@PathVariable String key) {
        try {
            boolean result = listCacheService.delete(key);
            if (result) {
                // 从热点数据控制器中移除键
            HotKeyController.removeKey("list", key);
                return CacheR.success(true, "删除List缓存成功");
            } else {
                return CacheR.fail("删除List缓存失败");
            }
        } catch (Exception e) {
            return CacheR.fail("删除List缓存异常: " + e.getMessage());
        }
    }

    /**
     * 设置List缓存过期时间
     *
     * @param key     List键
     * @param timeout 过期时间（秒）
     * @return 统一返回结果
     */
    @PostMapping("/{key}/expire")
    public CacheR<Boolean> expire(@PathVariable String key, @RequestParam long timeout) {
        try {
            boolean result = listCacheService.expire(key, timeout);
            if (result) {
                return CacheR.success(true, "设置过期时间成功");
            } else {
                return CacheR.fail("设置过期时间失败");
            }
        } catch (Exception e) {
            return CacheR.fail("设置过期时间异常: " + e.getMessage());
        }
    }
}